//
//  FRQEmailViewController.m
//  FRQBluetoothKit_Example
//
//  Created by admin on 2023/10/24.
//  Copyright © 2023 xuchunhai. All rights reserved.
//

#import "FRQEmailViewController.h"
#import "EmailViews.h"
@interface FRQEmailViewController ()
@property (nonatomic,strong) EmailViews *EmailView;
@end

@implementation FRQEmailViewController
- (EmailViews *)EmailView{
    if (nil==_EmailView) {
        _EmailView=skXibView(@"EmailViews");
        [self.view addSubview:_EmailView];
        [_EmailView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.mas_topLayoutGuide);
            make.bottom.mas_equalTo(self.mas_bottomLayoutGuide);
            make.right.left.mas_equalTo(0);
        }];
        NSMutableParagraphStyle *style1 = [[NSMutableParagraphStyle alloc] init];
        style1.minimumLineHeight = 0;
        NSAttributedString *attributedString1 = [[NSAttributedString alloc] initWithString:@"Password" attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:12], NSParagraphStyleAttributeName : style1,NSForegroundColorAttributeName:skUIColorFromRGB(0x939598)}];
        _EmailView.txtPassword.attributedPlaceholder = attributedString1;
        
        NSMutableParagraphStyle *style2 = [[NSMutableParagraphStyle alloc] init];
        style2.minimumLineHeight = 0;
        NSAttributedString *attributedString2 = [[NSAttributedString alloc] initWithString:@"New email" attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:12], NSParagraphStyleAttributeName : style2,NSForegroundColorAttributeName:skUIColorFromRGB(0x939598)}];
        _EmailView.txtNewemail.attributedPlaceholder = attributedString2;
        
        NSMutableParagraphStyle *style3 = [[NSMutableParagraphStyle alloc] init];
        style3.minimumLineHeight = 0;
        NSAttributedString *attributedString3 = [[NSAttributedString alloc] initWithString:@"Confirm new email" attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:12], NSParagraphStyleAttributeName : style3,NSForegroundColorAttributeName:skUIColorFromRGB(0x939598)}];
        _EmailView.txtConfirmnewemail.attributedPlaceholder = attributedString3;
        
        
        @weakify(self)
        [[_EmailView.btnChange rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(__kindof UIControl * _Nullable x) {
            @strongify(self)
//            app/user/updateEmail
//            修改邮箱
            [self updateEmail];
            
        }];
    }
    return _EmailView;
}
-(void)updateEmail{
    NSString *pwdC=[NSString stringWithFormat:@"%@",_EmailView.txtPassword.text];
    
    NSString *newEmail=[NSString stringWithFormat:@"%@",_EmailView.txtNewemail.text];
    NSString *newEmainCf=[NSString stringWithFormat:@"%@",_EmailView.txtConfirmnewemail.text];
    
    pwdC=[PKNetTools md5:pwdC];
    pwdC=[PKNetTools md5:pwdC];
    
    if([newEmail isEqualToString:newEmainCf]){
        
        NSDictionary *dic=@{@"accountNo":newEmail,
                            @"password":pwdC
        };
        [skNetTools skPOSTWithURL:[NSString stringWithFormat:@"%@%@",skURL,@"app/user/updateEmail"] parameters:dic cachePolicy:0 callback:^(id  _Nonnull responseObject, NSError * _Nonnull error) {
            NSLog(@"数据=%@",responseObject);
            skNetModel *model=responseObject;
            [SkToast SkToastShow:model.msg];
            if(model.code==200){
                _EmailView.labTip.hidden=NO;
                _EmailView.labTip.textColor=skphUIColorFromRGB(0x44cd2c,0.8);
                [_EmailView.viewContain mas_remakeConstraints:^(MASConstraintMaker *make) {
                    make.bottom.left.right.mas_equalTo(0);
                    make.top.mas_equalTo(self.mas_topLayoutGuide).offset(75+100);
                }];
                _EmailView.labTip.text=@"Your password has been updated \nsuccessfully.";
            }else{
                _EmailView.labTip.hidden=NO;
                _EmailView.labTip.textColor=skUIColorFromRGB(0xdd7023);
                [_EmailView.viewContain mas_remakeConstraints:^(MASConstraintMaker *make) {
                    make.bottom.left.right.mas_equalTo(0);
                    make.top.mas_equalTo(self.mas_topLayoutGuide).offset(75+100);
                }];
                _EmailView.labTip.text=@"Please fill in the fields properly \nand submit again.";
            }
        }];
    }else{
//        [SkToast SkToastShow:@"Please enter the relevant information of the modified email address"];
//        [APP.window makeToast:@"Please enter the relevant information of the modified email address"];
        _EmailView.labTip.hidden=NO;
        _EmailView.labTip.textColor=skUIColorFromRGB(0xdd7023);
        [_EmailView.viewContain mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.bottom.left.right.mas_equalTo(0);
            make.top.mas_equalTo(self.mas_topLayoutGuide).offset(75+100);
        }];
        _EmailView.labTip.text=@"Please fill in the fields properly \nand submit again.";
    }
    
    
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.EmailView.hidden=NO;
    self.title=@"Email";
    self.EmailView.txtEmail.text=skUser.accountNo;
    self.EmailView.txtEmail.enabled=NO;
    // Do any additional setup after loading the view.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
